import type { TodoAction } from '../action/todos';

type TodoList = {
  id: number;
  text: string;
  done: boolean;
}[];

const initialState: TodoList = [
  {
    id: 1,
    text: '吃饭',
    done: false,
  },
  {
    id: 2,
    text: '睡觉',
    done: true,
  },
  {
    id: 3,
    text: '敲代码',
    done: false,
  },
];

export const todos = (state = initialState, action: TodoAction): TodoList => {
  // 添加
  if (action.type === 'todos/add') {
    return [
      ...state,
      { id: state.length + 1, text: action.payload, done: false },
    ];
  }
  // 删除
  if (action.type === 'todos/del') {
    return state.filter((item) => item.id !== action.payload);
  }

  return state;
};
